<template>
  <el-container class="home">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/homeLogo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="outLogin" class="outLogin"
        >退出登录</el-button
      >
    </el-header>
    <!-- 页面主题区域 -->
    <el-container style="height: 80%">
      <!-- 页面侧边栏区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px  '">
        <div class="toggle" @click="toggleCut">|||</div>
        <el-menu
          background-color="#efefef"
          text-color="#333"
          active-text-color="skyblue"
          :unique-opened="true"
          :collapse-transition="false"
          :collapse="isCollapse"
          :router="true"
          :default-active="activePath"
        >
          <el-submenu
            :index="items.id + ''"
            v-for="items in menulist"
            :key="items.id"
          >
            <!-- 一级菜单区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconListObj[items.id]"></i>
              <!-- 文本 -->
              <span>{{ items.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="`/${itemChild.path}`"
              v-for="itemChild in items.children"
              :key="itemChild.id"
              @click="keepState(`/${itemChild.path}`)"
            >
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{ itemChild.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconListObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-lock_fill',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      // 激活后的链接地址
      activePath: ''
    }
  },
  created() {
    this.getMenulist()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录功能实现
    outLogin() {
      window.sessionStorage.clear()
      this.$router.push('/Login')
    },
    // 获取所有的菜单
    async getMenulist() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 点击按钮切换菜单的折叠与展开
    toggleCut() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    keepState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home {
  height: 100%;
  .el-header {
    background-color: #efefef;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    div {
      height: 100%;
      display: flex;
      align-items: center;
      img {
        height: 100%;
      }
      span {
        color: #333;
        font-size: 18px;
        font-weight: 700;
      }
    }
  }
  .el-aside {
    background-color: #efefef;
    .toggle {
      background-color: #f4f5f5;
      line-height: 24px;
      text-align: center;
      color: #333;
      font-size: 14px;
      letter-spacing: 0.2em;
      user-select: none;
      cursor: pointer;
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-main {
    background-color: #fff;
  }
}
.iconfont {
  margin-right: 10px;
}
</style>
